import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location) {
  return originalPush.call(this, location).catch(err => { })
};
Vue.use(VueRouter)

const routes = [
  // {
  //   path: '/',
  //   redirect: "/home"
  // },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    children: [
      {

        path: 'popup',
        name: 'About',
        component: () => import(/* webpackChunkName: "popup" */ '../components/home/Popup.vue'),
        meta: {
          activeStyle: 0
        }
      },


    ]
  },

  {
    path: '/user',
    name: 'User',
    component: () => import(/* webpackChunkName: "user" */ '../views/User.vue'),
    meta: {
      activeStyle: 4
    }
  },
  {
    path: '/category',
    name: 'Category',
    component: () => import(/* webpackChunkName: "category" */ '../views/Category.vue'),
    meta: {
      activeStyle: 2
    }
  },
  {
    path: '/cart',
    name: 'Cart',
    component: () => import(/* webpackChunkName: "cart" */ '../views/Cart.vue'),
    meta: {
      activeStyle: 3
    }
  },
  {
    path: '/topic',
    name: 'Topic',
    component: () => import(/* webpackChunkName: "topic" */ '../views/Topic.vue'),
    meta: {
      activeStyle: 1
    }
  },
  {
    path: '/productdetails',
    name: 'ProductDetails',
  
    component: () => import(/* webpackChunkName: "productdetails" */ '../views/ProductDetails.vue')
  },
  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// //路由守卫or路由拦截,能够在每次路径跳转之前拦截下来,做一定的逻辑
// router.beforeEach((to, from, next) => {
//   // from   从哪个页面开始
//   // to     去到哪个页面
//   // next() 允许跳转


//   //如果跳转到这个路径时
//   if (to.path == "/cart") {
//     //能获取到token的情况下
//     let token = localStorage.getItem("token")
//     if (token) { //true
//       console.log("进入购物车");
//       next()  //允许进入这个页面
//     } else {
//       // 否则,提示用户
//       Vue.prototype.$toast.success("请先登录")
//       setTimeout(() => {
//         next("/user")  //自动返回首页
//       }, 1000)
//     }
//     return
//   }
//   next()
// })

// router.beforeEach((to,from,next)=>{
//     if(to.path=="/cart"){
//         let token =localStorage.getItem("token")
//         if(token){
//             next()
//         }else{
//           Vue.prototype.$toast.success("请先登录")
//           setTimeout(()=>{
//             next("/user")
//           },1000)
//         }
//         return
//     }
//     next()
// })

export default router
